import xml.etree.ElementTree as ET

import cv2
import numpy as np
import spectral.io.envi as envi
from numpy import flip

dark_ref = envi.open('pathology/data/032370b-20x-roi6.hdr', 'pathology/data/032370b-20x-roi6.raw')
dark_nparr = np.array(dark_ref.load())
dark_nparr = flip(dark_nparr, 0)
tree = ET.parse('/home/liyuan/Programming/python/pathology/pathology/data/032370b-20x-roi6.xml')

polygon = tree.getroot().findall('object/polygon')

mask = np.zeros((dark_nparr.shape[0], dark_nparr.shape[1]), dtype="uint8")
for p in polygon:
    x = p.findall('pt/x')
    y = p.findall('pt/y')
    x_coor = list(map(lambda x:int(x.text), x))
    y_coor = list(map(lambda y:int(y.text), y))
c = []
for x, y in zip(x_coor, y_coor):
	c.append([x, y])
cor_xy = np.array(c)
# cor_xy = np.hstack(mas(x_coor, y_coor))
cv2.polylines(mask, np.int32([cor_xy]), 1, 1)
cv2.fillPoly(mask, np.int32([cor_xy]), 1)






IMAGE = np.flipud(dark_nparr)
GND = mask


liangxing = np.where(GND==0)
exing = np.where(GND==1)


for i in range(60):
    print(int(IMAGE[liangxing[0][0], liangxing[1][0]][i]))

print('*'*50)

for i in range(60):
    print(int(IMAGE[exing[0][0], exing[1][0]][i]))

print('end')
